﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class NewsDetail : System.Web.UI.PageEx
{
    private DataSet ds = null;
    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strSelectId = string.Empty;
    private string strSelectClassId = string.Empty;
    //////////qiu//////////////
    protected static string strMenu = string.Empty;
    ///////////////////////////
    protected void Page_Load(object sender, EventArgs e)
    {
        strSelectId = Convert.ToString(Request.QueryString["Id"]);
        strSelectClassId = Convert.ToString(Request.QueryString["ClassId"]);
        if (!Page.IsPostBack)
        {
            DataBinds();
        }
    }

    protected void DataBinds()
    {
        int iSelectId = 0;
        int.TryParse(strSelectId, out iSelectId);
        Model.DbArticles dbArticles = DalEx.DbArticlesEx.Select(iSelectId);
        Model.DbArticles _dbArticles = null;
        if (dbArticles != null)
        {
            Model.DbClass dbClass = DalEx.DbClassEx.Select(dbArticles.AClassId);
            //////////qiu//////////////
            strMenu = "园长工作室";
            if (!string.IsNullOrEmpty(Request.QueryString["ClassId"]))
            {
                if (Request.QueryString["ClassId"].ToString() == "1")
                {
                    strMenu = "工作室计划";
                }
                if (Request.QueryString["ClassId"].ToString() == "2")
                {
                    strMenu = "活动掠影";
                }
            }
            //////////////////////////
            if (dbClass != null)
            {
                lblClassName.Text = dbClass.CName;
                //////////qiu//////////////
                strMenu = dbClass.CName;
                ///////////////////////////
            }
            this.Title = dbArticles.ATitle;
            lblPubTime.Text = dbArticles.AAddTime.ToString("yyyy-MM-dd HH:mm:ss");
            litTitle.Text = string.IsNullOrEmpty(dbArticles.AHtmlTitle) ? dbArticles.ATitle : dbArticles.AHtmlTitle;
            litContents.Text = HttpUtility.HtmlDecode(dbArticles.ANote);
            litKeywords.Text = dbArticles.AKeywords;
            DalEx.DbArticlesEx.UpdateViews(dbArticles.AId);
            /////////////////////////显示上一篇
            litOthLink.Text = "<p class=\"pre_p\"><span>上一篇:</span><span id=\"preInfo\">";
            ds = SqlHelper.Select("select top 1 * from dbArticles where aId<" + dbArticles.AId + " and aStatus=1 and (aClassId=" + dbArticles.AClassId + " or aClassIds like '%|" + dbArticles.AClassId + "|%') order by aOrder asc,aId desc");
            if (SqlHelper.HasRow(ds))
            {
                _dbArticles = new Model.DbArticles(ds.Tables[0].Rows[0]);
                litOthLink.Text += "<a href=\"PlanDetail.aspx?ClassId=" + _dbArticles.AClassId + "&Id=" + _dbArticles.AId + "\" target=\"_blank\" title=\"" + _dbArticles.ATitle + "\">" + _dbArticles.ATitle + "</a>";
            }
            else
            {
                litOthLink.Text += "没有了!";
            }
            /////////////////////////显示下一篇
            litOthLink.Text += "</span></p><p class=\"next_p\"><span>下一篇:</span><span id=\"nextInfo\">";
            ds = SqlHelper.Select("select top 1 * from dbArticles where aId>" + dbArticles.AId + " and aStatus=1 and (aClassId=" + dbArticles.AClassId + " or aClassIds like '%|" + dbArticles.AClassId + "|%') order by aOrder desc,aId asc");
            if (SqlHelper.HasRow(ds))
            {
                _dbArticles = new Model.DbArticles(ds.Tables[0].Rows[0]);
                litOthLink.Text += "<a href=\"PlanDetail.aspx?ClassId=" + _dbArticles.AClassId + "&Id=" + _dbArticles.AId + "\" target=\"_blank\" title=\"" + _dbArticles.ATitle + "\">" + _dbArticles.ATitle + "</a>";
            }
            else
            {
                litOthLink.Text += "没有了!";
            }
            litOthLink.Text += "</span></p>";

            setMeta(dbArticles.ATitle, dbArticles.AKeywords, dbArticles.ADescription);
        }
        else
        {
            MessageBox1("抱歉，信息不存在或已被删除！", "/Default.aspx");
        }
    }
    protected void rptClassMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            HyperLink hlLinks = (HyperLink)e.Item.FindControl("hlLinks");
            string strCurrentId = Convert.ToString(drv["Cid"]);
            if (strCurrentId == strSelectClassId)
                hlLinks.Attributes.Add("class", "on");
        }
    }
    private void setMeta(string title, string key, string des)
    {
        string strWebTitle = string.Empty, strWebKeywords = string.Empty, strWebDescription = string.Empty;
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            strWebTitle = dbSystem.STitle;
            strWebKeywords = dbSystem.SKeywords;
            strWebDescription = dbSystem.SDescription;
        }
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = string.IsNullOrEmpty(key) ? strWebKeywords : key;

        this.Title = string.IsNullOrEmpty(title) ? strWebTitle : title;

        description.Name = "description";
        description.Content = string.IsNullOrEmpty(des) ? strWebDescription : des;

        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }
}